package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.YlBedMapper;
import com.caifu.mapper.YlFloorMapper;
import com.caifu.mapper.YlRoomMapper;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlBed;
import com.caifu.pojo.YlFloor;
import com.caifu.pojo.YlRoom;
import com.caifu.service.IBedService;
import com.caifu.util.Constance;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BedServiceImpl implements IBedService {

    @Resource
    private YlFloorMapper floorMapper;

    @Resource
    private YlRoomMapper roomMapper;

    @Resource
    private YlBedMapper bedMapper;

    @Override
    public JSONObject queryBedData(String roomNo) throws Exception {
        JSONObject result = new JSONObject();
        try {
            QueryWrapper<YlBed> bedQueryWrapper = new QueryWrapper<YlBed>();
            bedQueryWrapper.eq("ROOM_NO",roomNo);
            bedQueryWrapper.eq("BED_STATE","1");
            bedQueryWrapper.orderByAsc("BED_NAME");
            bedQueryWrapper.orderByAsc("CREATE_TIME");
            List<YlBed> bedList = bedMapper.selectList(bedQueryWrapper);
            int bedCount = 0;
            if(bedList!=null&&bedList.size()>0){
                bedCount =bedList.size();
            }
            result.put("code","0000");
            result.put("bedCount",bedCount);
            result.put("bedList",bedList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("bedCount",0);
            result.put("bedList",null);
            return result;
        }
    }

    @Override
    public JSONObject saveBedInfo(int bedCount, String roomNo
            , SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        if(null==user){
            result.put("code","1001");
            result.put("codeDesc","登录超时，请重新登录");
            return result;
        }
        if(bedCount<=0){
            result.put("code","1002");
            result.put("codeDesc","房间数量必须大于0");
            return result;
        }
        if(StringUtils.isBlank(roomNo)){
            result.put("code","1003");
            result.put("codeDesc","房间编号为空");
            return result;
        }
        try {
            //查询房间信息
            YlRoom room = roomMapper.selectById(roomNo);
            if(null==room){
                result.put("code","1004");
                result.put("codeDesc","房间信息不存在");
                return result;
            }
            //查询楼层信息
            YlFloor floor = floorMapper.selectById(room.getFloorNo());
            if(null==floor){
                result.put("code","1005");
                result.put("codeDesc","楼层信息不存在");
                return result;
            }
            String buildingNo = Constance.Null2EmptyTrim(floor.getBuildingNo());
            for(int i=0;i<bedCount;i++){
                //查询该楼层拥有过的最大床位号
                String maxBedName = bedMapper.queryMaxBedNameByBuildingNo(buildingNo,null);
                int totalBed= Constance.Null2EmptyTrimInt(maxBedName);
                int nameCount = bedMapper
                        .queryBedCountByBuildingNoAndBedName(Constance.Null2EmptyTrimInt(buildingNo)
                                ,totalBed+1+"");
                if(nameCount<=0){
                    YlBed bed = new YlBed();
                    bed.setBedName(totalBed+1);
                    bed.setBedEmpty("0"); //床位为待入住
                    bed.setBedState("1");
                    bed.setCreateTime(Constance.currentDateTime());
                    bed.setRoomNo(roomNo);
                    bed.setTellerNo(user.getUserNo()+"");
                    bedMapper.insert(bed);
                    if(bedCount==1){
                        result.put("bedNo",bed.getBedNo());
                        result.put("bedName",bed.getBedName());
                    }
                }
            }
            result.put("code","0000");
            result.put("codeDesc","添加成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","创建床位失败");
            return result;
        }
    }

    @Override
    public JSONObject queryBedDetails(String bedNo) throws Exception {
        JSONObject result = new JSONObject();
        YlBed bed = bedMapper.selectById(bedNo);
        if(null!=bed){
            result.put("bedNo",bed.getBedNo());
            result.put("bedName",bed.getBedName());
        }
        return result;
    }

    @Override
    public JSONObject updateBedInfo(YlBed bed) throws Exception {
        JSONObject result = new JSONObject();
        if(null==bed){
            result.put("code","1001");
            result.put("codeDesc","房间信息不存在");
            return result;
        }
        try {
            YlBed oldBed = bedMapper.selectById(bed.getBedNo());
            int oldBedName =oldBed.getBedName();
            int newBedName =bed.getBedName();
            if(oldBedName!=newBedName){
//                //根据床位编号查询楼苑编号
//                int buildingNo = bedMapper.queryBuildingNoByBedNo(oldBed.getBedNo());
//                //验证床位名称是否重复
//                int exitBed = bedMapper
//                        .queryBedCountByBuildingNoAndBedName(buildingNo,newBedName+"");
                //查询该房间下床位名称是否窜在
                int exitBed = bedMapper.queryBedCountByBedNameAndRoomNo(oldBed.getRoomNo()
                        ,newBedName+"");
                if(exitBed>0){
                    result.put("code","1003");
                    result.put("codeDesc","该床位名称已存在！");
                    return result;
                }
            }
            oldBed.setBedName(newBedName);
            bedMapper.updateById(oldBed);
            result.put("code","0000");
            result.put("codeDesc","修改成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","修改失败");
            return result;
        }
    }

    @Override
    public JSONObject deleteBedInfo(String bedNo) throws Exception {
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(bedNo)){
            result.put("code","1001");
            result.put("codeDesc","床位编号为空");
            return result;
        }
        try {
            YlBed oldBed = bedMapper.selectById(bedNo);
            if(null==oldBed){
                result.put("code","1002");
                result.put("codeDesc","房间信息不存在");
                return result;
            }
            if(!"0".equals(oldBed.getBedEmpty())){
                result.put("code","1003");
                result.put("codeDesc","该床位在使用中，无法删除！");
                return result;
            }
            oldBed.setBedState("9");
            bedMapper.updateById(oldBed);
            result.put("code","0000");
            result.put("codeDesc","删除成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","删除失败");
            return result;
        }
    }
}
